home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / textutl3.lha / textutils-1.3 / src / tail.c < prev    next >
C/C++ Source or Header  |  1992-06-29  |  21KB  |  859 lines

  1. /* tail -- output last part of file(s)
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Can display any amount of data, unlike the Unix version, which uses
  19.    a fixed size buffer and therefore can only deliver a limited number
  20.    of lines.
  21.  
  22.    Options:
  23.    -b            Tail by N 512-byte blocks.
  24.    -c, --bytes=N[bkm]    Tail by N bytes
  25.             [or 512-byte blocks, kilobytes, or megabytes].
  26.    -f, --follow        Loop forever trying to read more characters at the
  27.             end of the file, on the assumption that the file
  28.             is growing.  Ignored if reading from a pipe.
  29.             Cannot be used if more than one file is given.
  30.    -k            Tail by N kilobytes.
  31.    -N, -l, -n, --lines=N    Tail by N lines.
  32.    -m            Tail by N megabytes.
  33.    -q, --quiet, --silent    Never print filename headers.
  34.    -v, --verbose        Always print filename headers.
  35.  
  36.    If a number (N) starts with a `+', begin printing with the Nth item
  37.    from the start of each file, instead of from the end.
  38.  
  39.    Reads from standard input if no files are given or when a filename of
  40.    ``-'' is encountered.
  41.    By default, filename headers are printed only more than one file
  42.    is given.
  43.    By default, prints the last 10 lines (tail -n 10).
  44.  
  45.    Original version by Paul Rubin <phr@ocf.berkeley.edu>.
  46.    Extensions by David MacKenzie <djm@ai.mit.edu>. */
  47.  
  48. #include <stdio.h>
  49. #include <getopt.h>
  50. #include <ctype.h>
  51. #include <sys/types.h>
  52. #include "system.h"
  53.  
  54. #ifdef isascii
  55. #define ISDIGIT(c) (isascii ((c)) && isdigit ((c)))
  56. #else
  57. #define ISDIGIT(c) (isdigit ((c)))
  58. #endif
  59.  
  60. /* Number of items to tail. */
  61. #define DEFAULT_NUMBER 10
  62.  
  63. /* Size of atomic reads. */
  64. #define BUFSIZE (512 * 8)
  65.  
  66. /* Number of bytes per item we are printing.
  67.    If 0, tail in lines. */
  68. int unit_size;
  69.  
  70. /* If nonzero, read from end of file until killed. */
  71. int forever;
  72.  
  73. /* If nonzero, count from start of file instead of end. */
  74. int from_start;
  75.  
  76. /* If nonzero, print filename headers. */
  77. int print_headers;
  78.  
  79. /* When to print the filename banners. */
  80. enum header_mode
  81. {
  82.   multiple_files, always, never
  83. };
  84.  
  85. char *xmalloc ();
  86. int file_lines ();
  87. int pipe_bytes ();
  88. int pipe_lines ();
  89. int start_bytes ();
  90. int start_lines ();
  91. int tail ();
  92. int tail_bytes ();
  93. int tail_file ();
  94. int tail_lines ();
  95. long atou();
  96. void dump_remainder ();
  97. void error ();
  98. void parse_unit ();
  99. void usage ();
  100. void write_header ();
  101. void xwrite ();
  102.  
  103. /* The name this program was run with. */
  104. char *program_name;
  105.  
  106. /* Nonzero if we have ever read standard input. */
  107. int have_read_stdin;
  108.  
  109. struct option long_options[] =
  110. {
  111.   {"bytes", 1, NULL, 'c'},
  112.   {"follow", 0, NULL, 'f'},
  113.   {"lines", 1, NULL, 'n'},
  114.   {"quiet", 0, NULL, 'q'},
  115.   {"silent", 0, NULL, 'q'},
  116.   {"verbose", 0, NULL, 'v'},
  117.   {NULL, 0, NULL, 0}
  118. };
  119.  
  120. void
  121. main (argc, argv)
  122.      int argc;
  123.      char **argv;
  124. {
  125.   enum header_mode header_mode = multiple_files;
  126.   int exit_status = 0;
  127.   /* If from_start, the number of items to skip before printing; otherwise,
  128.      the number of items at the end of the file to print.  Initially, -1
  129.      means the value has not been set. */
  130.   long number = -1;
  131.   int c;            /* Option character. */
  132.  
  133.   program_name = argv[0];
  134.   have_read_stdin = 0;
  135.   unit_size = 0;
  136.   forever = from_start = print_headers = 0;
  137.  
  138.   if (argc > 1
  139.       && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  140.       || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
  141.     {
  142.       /* Old option syntax: a dash or plus, one or more digits (zero digits
  143.      are acceptable with a plus), and one or more option letters. */
  144.       if (argv[1][0] == '+')
  145.     from_start = 1;
  146.       if (argv[1][1] != 0)
  147.     {
  148.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  149.         number = number * 10 + *argv[1] - '0';
  150.       /* Parse any appended option letters. */
  151.       while (*argv[1])
  152.         {
  153.           switch (*argv[1])
  154.         {
  155.         case 'b':
  156.           unit_size = 512;
  157.           break;
  158.  
  159.         case 'c':
  160.           unit_size = 1;
  161.           break;
  162.  
  163.         case 'f':
  164.           forever = 1;
  165.           break;
  166.  
  167.         case 'k':
  168.           unit_size = 1024;
  169.           break;
  170.  
  171.         case 'l':
  172.           unit_size = 0;
  173.           break;
  174.  
  175.         case 'm':
  176.           unit_size = 1048576;
  177.           break;
  178.  
  179.         case 'q':
  180.           header_mode = never;
  181.           break;
  182.  
  183.         case 'v':
  184.           header_mode = always;
  185.           break;
  186.  
  187.         default:
  188.           error (0, 0, "unrecognized option `-%c'", *argv[1]);
  189.           usage ();
  190.         }
  191.           ++argv[1];
  192.         }
  193.     }
  194.       /* Make the options we just parsed invisible to getopt. */
  195.       argv[1] = argv[0];
  196.       argv++;
  197.       argc--;
  198.     }
  199.  
  200.   while ((c = getopt_long (argc, argv, "c:n:fqv", long_options, (int *) 0))
  201.      != EOF)
  202.     {
  203.       switch (c)
  204.     {
  205.     case 'c':
  206.       unit_size = 1;
  207.       parse_unit (optarg);
  208.       goto getnum;
  209.     case 'n':
  210.       unit_size = 0;
  211.     getnum:
  212.       if (*optarg == '+')
  213.         {
  214.           from_start = 1;
  215.           ++optarg;
  216.         }
  217.       else if (*optarg == '-')
  218.         ++optarg;
  219.       number = atou (optarg);
  220.       if (number == -1)
  221.         error (1, 0, "invalid number `%s'", optarg);
  222.       break;
  223.  
  224.     case 'f':
  225.       forever = 1;
  226.       break;
  227.  
  228.     case 'q':
  229.       header_mode = never;
  230.       break;
  231.  
  232.     case 'v':
  233.       header_mode = always;
  234.       break;
  235.  
  236.     default:
  237.       usage ();
  238.     }
  239.     }
  240.  
  241.   if (number == -1)
  242.     number = DEFAULT_NUMBER;
  243.  
  244.   /* To start printing with item `number' from the start of the file, skip
  245.      `number' - 1 items.  `tail +0' is actually meaningless, but for Unix
  246.      compatibility it's treated the same as `tail +1'. */
  247.   if (from_start)
  248.     {
  249.       if (number)
  250.     --number;
  251.     }
  252.  
  253.   if (unit_size > 1)
  254.     number *= unit_size;
  255.  
  256.   if (optind < argc - 1 && forever)
  257.     error (1, 0, "cannot follow the ends of multiple files");
  258.  
  259.   if (header_mode == always
  260.       || (header_mode == multiple_files && optind < argc - 1))
  261.     print_headers = 1;
  262.  
  263.   if (optind == argc)
  264.     exit_status |= tail_file ("-", number);
  265.  
  266.   for (; optind < argc; ++optind)
  267.     exit_status |= tail_file (argv[optind], number);
  268.  
  269.   if (have_read_stdin && close (0) < 0)
  270.     error (1, errno, "-");
  271.   if (close (1) < 0)
  272.     error (1, errno, "write error");
  273.   exit (exit_status);
  274. }
  275.  
  276. /* Display the last NUMBER units of file FILENAME.
  277.    "-" for FILENAME means the standard input.
  278.    Return 0 if successful, 1 if an error occurred. */
  279.  
  280. int
  281. tail_file (filename, number)
  282.      char *filename;
  283.      long number;
  284. {
  285.   int fd;
  286.  
  287.   if (!strcmp (filename, "-"))
  288.     {
  289.       have_read_stdin = 1;
  290.       filename = "standard input";
  291.       if (print_headers)
  292.     write_header (filename);
  293.       return tail (filename, 0, number);
  294.     }
  295.   else
  296.     {
  297.       fd = open (filename, O_RDONLY);
  298.       if (fd >= 0)
  299.     {
  300.       int errors;
  301.  
  302.       if (print_headers)
  303.         write_header (filename);
  304.       errors = tail (filename, fd, number);
  305.       if (close (fd) == 0)
  306.         return errors;
  307.     }
  308.       error (0, errno, "%s", filename);
  309.       return 1;
  310.     }
  311. }
  312.  
  313. void
  314. write_header (filename)
  315.      char *filename;
  316. {
  317.   static int first_file = 1;
  318.  
  319.   if (first_file)
  320.     {
  321.       xwrite (1, "==> ", 4);
  322.       first_file = 0;
  323.     }
  324.   else
  325.     xwrite (1, "\n==> ", 5);
  326.   xwrite (1, filename, strlen (filename));
  327.   xwrite (1, " <==\n", 5);
  328. }
  329.  
  330. /* Display the last NUMBER units of file FILENAME, open for reading
  331.    in FD.
  332.    Return 0 if successful, 1 if an error occurred. */
  333.  
  334. int
  335. tail (filename, fd, number)
  336.      char *filename;
  337.      int fd;
  338.      long number;
  339. {
  340.   if (unit_size)
  341.     return tail_bytes (filename, fd, number);
  342.   else
  343.     return tail_lines (filename, fd, number);
  344. }
  345.  
  346. /* Display the last part of file FILENAME, open for reading in FD,
  347.    using NUMBER characters.
  348.    Return 0 if successful, 1 if an error occurred. */
  349.  
  350. int
  351. tail_bytes (filename, fd, number)
  352.      char *filename;
  353.      int fd;
  354.      long number;
  355. {
  356.   struct stat stats;
  357.  
  358.   /* Use fstat instead of checking for errno == ESPIPE because
  359.      lseek doesn't work on some special files but doesn't return an
  360.      error, either. */
  361.   if (fstat (fd, &stats))
  362.     {
  363.       error (0, errno, "%s", filename);
  364.       return 1;
  365.     }
  366.  
  367.   if (from_start)
  368.     {
  369.       if (S_ISREG (stats.st_mode))
  370.     lseek (fd, number, SEEK_SET);
  371.       else if (start_bytes (filename, fd, number))
  372.     return 1;
  373.       dump_remainder (filename, fd);
  374.     }
  375.   else
  376.     {
  377.       if (S_ISREG (stats.st_mode))
  378.     {
  379.       if (lseek (fd, 0L, SEEK_END) <= number)
  380.         /* The file is shorter than we want, or just the right size, so
  381.            print the whole file. */
  382.         lseek (fd, 0L, SEEK_SET);
  383.       else
  384.         /* The file is longer than we want, so go back. */
  385.         lseek (fd, -number, SEEK_END);
  386.       dump_remainder (filename, fd);
  387.     }
  388.       else
  389.     return pipe_bytes (filename, fd, number);
  390.     }
  391.   return 0;
  392. }
  393.  
  394. /* Display the last part of file FILENAME, open for reading on FD,
  395.    using NUMBER lines.
  396.    Return 0 if successful, 1 if an error occurred. */
  397.  
  398. int
  399. tail_lines (filename, fd, number)
  400.      char *filename;
  401.      int fd;
  402.      long number;
  403. {
  404.   struct stat stats;
  405.   long length;
  406.  
  407.   if (fstat (fd, &stats))
  408.     {
  409.       error (0, errno, "%s", filename);
  410.       return 1;
  411.     }
  412.  
  413.   if (from_start)
  414.     {
  415.       if (start_lines (filename, fd, number))
  416.     return 1;
  417.       dump_remainder (filename, fd);
  418.     }
  419.   else
  420.     {
  421.       if (S_ISREG (stats.st_mode))
  422.     {
  423.       length = lseek (fd, 0L, SEEK_END);
  424.       if (length != 0 && file_lines (filename, fd, number, length))
  425.         return 1;
  426.       dump_remainder (filename, fd);
  427.     }
  428.       else
  429.     return pipe_lines (filename, fd, number);
  430.     }
  431.   return 0;
  432. }
  433.  
  434. /* Print the last NUMBER lines from the end of file FD.
  435.    Go backward through the file, reading `BUFSIZE' bytes at a time (except
  436.    probably the first), until we hit the start of the file or have
  437.    read NUMBER newlines.
  438.    POS starts out as the length of the file (the offset of the last
  439.    byte of the file + 1).
  440.    Return 0 if successful, 1 if an error occurred. */
  441.  
  442. int
  443. file_lines (filename, fd, number, pos)
  444.      char *filename;
  445.      int fd;
  446.      long number;
  447.      long pos;
  448. {
  449.   char buffer[BUFSIZE];
  450.   int bytes_read;
  451.   int i;            /* Index into `buffer' for scanning. */
  452.  
  453.   if (number == 0)
  454.     return 0;
  455.  
  456.   /* Set `bytes_read' to the size of the last, probably partial, buffer;
  457.      0 < `bytes_read' <= `BUFSIZE'. */
  458.   bytes_read = pos % BUFSIZE;
  459.   if (bytes_read == 0)
  460.     bytes_read = BUFSIZE;
  461.   /* Make `pos' a multiple of `BUFSIZE' (0 if the file is short), so that all
  462.      reads will be on block boundaries, which might increase efficiency. */
  463.   pos -= bytes_read;
  464.   lseek (fd, pos, SEEK_SET);
  465.   bytes_read = read (fd, buffer, bytes_read);
  466.   if (bytes_read == -1)
  467.     {
  468.       error (0, errno, "%s", filename);
  469.       return 1;
  470.     }
  471.  
  472.   /* Count the incomplete line on files that don't end with a newline. */
  473.   if (bytes_read && buffer[bytes_read - 1] != '\n')
  474.     --number;
  475.  
  476.   do
  477.     {
  478.       /* Scan backward, counting the newlines in this bufferfull. */
  479.       for (i = bytes_read - 1; i >= 0; i--)
  480.     {
  481.       /* Have we counted the requested number of newlines yet? */
  482.       if (buffer[i] == '\n' && number-- == 0)
  483.         {
  484.           /* If this newline wasn't the last character in the buffer,
  485.              print the text after it. */
  486.           if (i != bytes_read - 1)
  487.         xwrite (1, &buffer[i + 1], bytes_read - (i + 1));
  488.           return 0;
  489.         }
  490.     }
  491.       /* Not enough newlines in that bufferfull. */
  492.       if (pos == 0)
  493.     {
  494.       /* Not enough lines in the file; print the entire file. */
  495.       lseek (fd, 0L, SEEK_SET);
  496.       return 0;
  497.     }
  498.       pos -= BUFSIZE;
  499.       lseek (fd, pos, SEEK_SET);
  500.     }
  501.   while ((bytes_read = read (fd, buffer, BUFSIZE)) > 0);
  502.   if (bytes_read == -1)
  503.     {
  504.       error (0, errno, "%s", filename);
  505.       return 1;
  506.     }
  507.   return 0;
  508. }
  509.  
  510. /* Print the last NUMBER lines from the end of the standard input,
  511.    open for reading as pipe FD.
  512.    Buffer the text as a linked list of LBUFFERs, adding them as needed.
  513.    Return 0 if successful, 1 if an error occured. */
  514.  
  515. int
  516. pipe_lines (filename, fd, number)
  517.      char *filename;
  518.      int fd;
  519.      long number;
  520. {
  521.   struct linebuffer
  522.   {
  523.     int nbytes, nlines;
  524.     char buffer[BUFSIZE];
  525.     struct linebuffer *next;
  526.   };
  527.   typedef struct linebuffer LBUFFER;
  528.   LBUFFER *first, *last, *tmp;
  529.   int i;            /* Index into buffers. */
  530.   int total_lines = 0;        /* Total number of newlines in all buffers. */
  531.   int errors = 0;
  532.  
  533.   first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  534.   first->nbytes = first->nlines = 0;
  535.   first->next = NULL;
  536.   tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  537.  
  538.   /* Input is always read into a fresh buffer. */
  539.   while ((tmp->nbytes = read (fd, tmp->buffer, BUFSIZE)) > 0)
  540.     {
  541.       tmp->nlines = 0;
  542.       tmp->next = NULL;
  543.  
  544.       /* Count the number of newlines just read. */
  545.       for (i = 0; i < tmp->nbytes; i++)
  546.     if (tmp->buffer[i] == '\n')
  547.       ++tmp->nlines;
  548.       total_lines += tmp->nlines;
  549.  
  550.       /* If there is enough room in the last buffer read, just append the new
  551.          one to it.  This is because when reading from a pipe, `nbytes' can
  552.          often be very small. */
  553.       if (tmp->nbytes + last->nbytes < BUFSIZE)
  554.     {
  555.       bcopy (tmp->buffer, &last->buffer[last->nbytes], tmp->nbytes);
  556.       last->nbytes += tmp->nbytes;
  557.       last->nlines += tmp->nlines;
  558.     }
  559.       else
  560.     {
  561.       /* If there's not enough room, link the new buffer onto the end of
  562.          the list, then either free up the oldest buffer for the next
  563.          read if that would leave enough lines, or else malloc a new one.
  564.          Some compaction mechanism is possible but probably not
  565.          worthwhile. */
  566.       last = last->next = tmp;
  567.       if (total_lines - first->nlines > number)
  568.         {
  569.           tmp = first;
  570.           total_lines -= first->nlines;
  571.           first = first->next;
  572.         }
  573.       else
  574.         tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  575.     }
  576.     }
  577.   if (tmp->nbytes == -1)
  578.     {
  579.       error (0, errno, "%s", filename);
  580.       errors = 1;
  581.       free ((char *) tmp);
  582.       goto free_lbuffers;
  583.     }
  584.  
  585.   free ((char *) tmp);
  586.  
  587.   /* This prevents a core dump when the pipe contains no newlines. */
  588.   if (number == 0)
  589.     goto free_lbuffers;
  590.  
  591.   /* Count the incomplete line on files that don't end with a newline. */
  592.   if (last->buffer[last->nbytes - 1] != '\n')
  593.     {
  594.       ++last->nlines;
  595.       ++total_lines;
  596.     }
  597.  
  598.   /* Run through the list, printing lines.  First, skip over unneeded
  599.      buffers. */
  600.   for (tmp = first; total_lines - tmp->nlines > number; tmp = tmp->next)
  601.     total_lines -= tmp->nlines;
  602.  
  603.   /* Find the correct beginning, then print the rest of the file. */
  604.   if (total_lines > number)
  605.     {
  606.       char *cp;
  607.  
  608.       /* Skip `total_lines' - `number' newlines.  We made sure that
  609.          `total_lines' - `number' <= `tmp->nlines'. */
  610.       cp = tmp->buffer;
  611.       for (i = total_lines - number; i; --i)
  612.     while (*cp++ != '\n')
  613.       /* Do nothing. */ ;
  614.       i = cp - tmp->buffer;
  615.     }
  616.   else
  617.     i = 0;
  618.   xwrite (1, &tmp->buffer[i], tmp->nbytes - i);
  619.  
  620.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  621.     xwrite (1, tmp->buffer, tmp->nbytes);
  622.  
  623. free_lbuffers:
  624.   while (first)
  625.     {
  626.       tmp = first->next;
  627.       free ((char *) first);
  628.       first = tmp;
  629.     }
  630.   return errors;
  631. }
  632.  
  633. /* Print the last NUMBER characters from the end of pipe FD.
  634.    This is a stripped down version of pipe_lines.
  635.    Return 0 if successful, 1 if an error occurred. */
  636.  
  637. int
  638. pipe_bytes (filename, fd, number)
  639.      char *filename;
  640.      int fd;
  641.      long number;
  642. {
  643.   struct charbuffer
  644.   {
  645.     int nbytes;
  646.     char buffer[BUFSIZE];
  647.     struct charbuffer *next;
  648.   };
  649.   typedef struct charbuffer CBUFFER;
  650.   CBUFFER *first, *last, *tmp;
  651.   int i;            /* Index into buffers. */
  652.   int total_bytes = 0;        /* Total characters in all buffers. */
  653.   int errors = 0;
  654.  
  655.   first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  656.   first->nbytes = 0;
  657.   first->next = NULL;
  658.   tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  659.  
  660.   /* Input is always read into a fresh buffer. */
  661.   while ((tmp->nbytes = read (fd, tmp->buffer, BUFSIZE)) > 0)
  662.     {
  663.       tmp->next = NULL;
  664.  
  665.       total_bytes += tmp->nbytes;
  666.       /* If there is enough room in the last buffer read, just append the new
  667.          one to it.  This is because when reading from a pipe, `nbytes' can
  668.          often be very small. */
  669.       if (tmp->nbytes + last->nbytes < BUFSIZE)
  670.     {
  671.       bcopy (tmp->buffer, &last->buffer[last->nbytes], tmp->nbytes);
  672.       last->nbytes += tmp->nbytes;
  673.     }
  674.       else
  675.     {
  676.       /* If there's not enough room, link the new buffer onto the end of
  677.          the list, then either free up the oldest buffer for the next
  678.          read if that would leave enough characters, or else malloc a new
  679.          one.  Some compaction mechanism is possible but probably not
  680.          worthwhile. */
  681.       last = last->next = tmp;
  682.       if (total_bytes - first->nbytes > number)
  683.         {
  684.           tmp = first;
  685.           total_bytes -= first->nbytes;
  686.           first = first->next;
  687.         }
  688.       else
  689.         {
  690.           tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  691.         }
  692.     }
  693.     }
  694.   if (tmp->nbytes == -1)
  695.     {
  696.       error (0, errno, "%s", filename);
  697.       errors = 1;
  698.       free ((char *) tmp);
  699.       goto free_cbuffers;
  700.     }
  701.  
  702.   free ((char *) tmp);
  703.  
  704.   /* Run through the list, printing characters.  First, skip over unneeded
  705.      buffers. */
  706.   for (tmp = first; total_bytes - tmp->nbytes > number; tmp = tmp->next)
  707.     total_bytes -= tmp->nbytes;
  708.  
  709.   /* Find the correct beginning, then print the rest of the file.
  710.      We made sure that `total_bytes' - `number' <= `tmp->nbytes'. */
  711.   if (total_bytes > number)
  712.     i = total_bytes - number;
  713.   else
  714.     i = 0;
  715.   xwrite (1, &tmp->buffer[i], tmp->nbytes - i);
  716.  
  717.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  718.     xwrite (1, tmp->buffer, tmp->nbytes);
  719.  
  720. free_cbuffers:
  721.   while (first)
  722.     {
  723.       tmp = first->next;
  724.       free ((char *) first);
  725.       first = tmp;
  726.     }
  727.   return errors;
  728. }
  729.  
  730. /* Skip NUMBER characters from the start of pipe FD, and print
  731.    any extra characters that were read beyond that.
  732.    Return 1 on error, 0 if ok.  */
  733.  
  734. int
  735. start_bytes (filename, fd, number)
  736.      char *filename;
  737.      int fd;
  738.      long number;
  739. {
  740.   char buffer[BUFSIZE];
  741.   int bytes_read = 0;
  742.  
  743.   while (number > 0 && (bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  744.     number -= bytes_read;
  745.   if (bytes_read == -1)
  746.     {
  747.       error (0, errno, "%s", filename);
  748.       return 1;
  749.     }
  750.   else if (number < 0)
  751.     xwrite (1, &buffer[bytes_read + number], -number);
  752.   return 0;
  753. }
  754.  
  755. /* Skip NUMBER lines at the start of file or pipe FD, and print
  756.    any extra characters that were read beyond that.
  757.    Return 1 on error, 0 if ok.  */
  758.  
  759. int
  760. start_lines (filename, fd, number)
  761.      char *filename;
  762.      int fd;
  763.      long number;
  764. {
  765.   char buffer[BUFSIZE];
  766.   int bytes_read = 0;
  767.   int bytes_to_skip = 0;
  768.  
  769.   while (number && (bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  770.     {
  771.       bytes_to_skip = 0;
  772.       while (bytes_to_skip < bytes_read)
  773.     if (buffer[bytes_to_skip++] == '\n' && --number == 0)
  774.       break;
  775.     }
  776.   if (bytes_read == -1)
  777.     {
  778.       error (0, errno, "%s", filename);
  779.       return 1;
  780.     }
  781.   else if (bytes_to_skip < bytes_read)
  782.     xwrite (1, &buffer[bytes_to_skip], bytes_read - bytes_to_skip);
  783.   return 0;
  784. }
  785.  
  786. /* Display file FILENAME from the current position in FD
  787.    to the end.  If `forever' is nonzero, keep reading from the
  788.    end of the file until killed. */
  789.  
  790. void
  791. dump_remainder (filename, fd)
  792.      char *filename;
  793.      int fd;
  794. {
  795.   char buffer[BUFSIZE];
  796.   int bytes_read;
  797.  
  798. output:
  799.   while ((bytes_read = read (fd, buffer, BUFSIZE)) > 0)
  800.     xwrite (1, buffer, bytes_read);
  801.   if (bytes_read == -1)
  802.     error (1, errno, "%s", filename);
  803.   if (forever)
  804.     {
  805.       sleep (1);
  806.       goto output;
  807.     }
  808. }
  809.  
  810. void
  811. parse_unit (str)
  812.      char *str;
  813. {
  814.   int arglen = strlen (str);
  815.  
  816.   if (arglen == 0)
  817.     return;
  818.  
  819.   switch (str[arglen - 1])
  820.     {
  821.     case 'b':
  822.       unit_size = 512;
  823.       str[arglen - 1] = '\0';
  824.       break;
  825.     case 'k':
  826.       unit_size = 1024;
  827.       str[arglen - 1] = '\0';
  828.       break;
  829.     case 'm':
  830.       unit_size = 1048576;
  831.       str[arglen - 1] = '\0';
  832.       break;
  833.     }
  834. }
  835.  
  836. /* Convert STR, a string of ASCII digits, into an unsigned integer.
  837.    Return -1 if STR does not represent a valid unsigned integer. */
  838.  
  839. long
  840. atou (str)
  841.      char *str;
  842. {
  843.   unsigned long value;
  844.  
  845.   for (value = 0; ISDIGIT (*str); ++str)
  846.     value = value * 10 + *str - '0';
  847.   return *str ? -1 : value;
  848. }
  849.  
  850. void
  851. usage ()
  852. {
  853.   fprintf (stderr, "\
  854. Usage: %s [-c [+]N[bkm]] [-n [+]N] [-fqv] [--bytes=[+]N[bkm]] [--lines=[+]N]\n\
  855.        [--follow] [--quiet] [--silent] [--verbose] [file...]\n\
  856.        %s [{-,+}Nbcfklmqv] [file...]\n", program_name, program_name);
  857.   exit (1);
  858. }
  859.